package fei.typhoon.engine.utils;

public class FeiColor {

	public static final byte GRADIENT_PLUS = 1;
	public static final byte GRADIENT_LESS = 2;

	/**
	 * 输入一个颜色,将它拆成四个部分: 透明度,红色,绿色和蓝色
	 * 
	 * @param color
	 *            将被拆分的颜色
	 * @return 拆分后的颜色数组，rgb[0]透明度，rgb[1]红色，rgb[2]绿色，rgb[3]蓝色
	 */
	public static int[] getARGB(int color) {
		int[] argb = new int[4];
		argb[0] = ((color & 0xFF000000) >> 24) & 0xFF;
		argb[1] = (color & 0x00FF0000) >> 16;
		argb[2] = (color & 0x0000FF00) >> 8;
		argb[3] = (color & 0x000000FF);
		return argb;
	}

	/**
	 * 透明度,红色,绿色和蓝色组合成颜色值
	 * 
	 * @param a
	 *            透明度
	 * @param r
	 *            红色
	 * @param g
	 *            绿色
	 * @param b
	 *            蓝色
	 * @return
	 */
	public static int getColor(int a, int r, int g, int b) {
		if (a < 0 || a > 0xFF || r < 0 || r > 0xFF || g < 0 || g > 0xFF
				|| b < 0 || b > 0xFF) {
			return 0xFF000000;
		}
		return a << 24 | r << 16 | g << 8 | b;
	}

	/**
	 * @param startColor
	 *            起始颜色值
	 * @param endColor
	 *            渐变结束颜色值
	 * @param steps
	 *            渐变步数
	 * @return
	 */
	public static int[] getGradientColors(int startColor, int endColor,
			int steps) {
		int[] startARGB = getARGB(startColor);
		int[] endARGB = getARGB(endColor);
		if (steps < 3) {
			return null;
		}
		int[] colors = new int[steps];
		colors[0] = startColor;
		colors[colors.length - 1] = endColor;
		steps = steps - 2;
		int alphaDiff = endARGB[0] - startARGB[0];
		int redDiff = endARGB[1] - startARGB[1];
		int greenDiff = endARGB[2] - startARGB[2];
		int blueDiff = endARGB[3] - startARGB[3];
		// 将第二到倒数第二个颜色值求出来
		for (int i = 1; i < colors.length - 1; i++) {
			colors[i] = getColor(startARGB[0] + alphaDiff * i / steps,
					startARGB[1] + redDiff * i / steps, startARGB[2]
							+ greenDiff * i / steps, startARGB[2] + blueDiff
							* i / steps);
		}
		return colors;
	}

	/**
	 * @param startColor
	 *            起始颜色值
	 * @param steps
	 *            渐变步数
	 * @param stepR
	 *            渐变每一步的跳跃R数值
	 * @param stepG
	 *            渐变每一步的跳跃G数值
	 * @param stepB
	 *            渐变每一步的跳跃B数值
	 * @return
	 */
	public static int[] getGradientColors(int startColor, int steps,
			float stepR, float stepG, float stepB) {
		int[] startARGB = getARGB(startColor);
		if (steps < 3) {
			return null;
		}
		int[] colors = new int[steps];
		colors[0] = startColor;
		steps = steps - 1;
		// 将第二到倒数第二个颜色值求出来
		for (int i = 1; i < colors.length; i++) {
			colors[i] = getColor(startARGB[0],
					(int) (startARGB[1] + stepR * i),
					(int) (startARGB[2] + stepG * i),
					(int) (startARGB[3] + stepB * i));
		}
		return colors;
	}

	/**
	 * @param startColor
	 *            起始颜色值
	 * @param steps
	 *            渐变步数
	 * @param stepR
	 *            渐变每一步的跳跃R数值
	 * @param stepG
	 *            渐变每一步的跳跃G数值
	 * @param stepB
	 *            渐变每一步的跳跃B数值
	 * @param gradient
	 *            颜色深浅渐变方向
	 * @param colorBack
	 *            颜色渐变是否回滚
	 * @return
	 */
	public static int[] getGradientColors(int startColor, int steps,
			float stepR, float stepG, float stepB, byte gradient,
			boolean colorBack) {
		int[] startARGB = getARGB(startColor);
		if (steps < 3) {
			return null;
		}
		int[] colors = new int[steps];
		colors[0] = startColor;
		steps = steps - 1;
		// 将第二到倒数第二个颜色值求出来
		for (int i = 1; i < colors.length; i++) {
			int delta = i;
			if (colorBack) {
				if (colors.length % 2 == 0) {
					if (i > (colors.length / 2) - 1) {
						delta = (colors.length / 2) - (i - (colors.length / 2))
								- 1;
					}
				} else {
					if (i > (colors.length / 2)) {
						delta = (colors.length / 2) - (i - (colors.length / 2));
					}
				}
			}
			if (gradient == GRADIENT_PLUS) {
				colors[i] = getColor(startARGB[0], (int) (startARGB[1] + stepR
						* delta), (int) (startARGB[2] + stepG * delta),
						(int) (startARGB[3] + stepB * delta));
			} else {
				colors[i] = getColor(startARGB[0], (int) (startARGB[1] - stepR
						* delta), (int) (startARGB[2] - stepG * delta),
						(int) (startARGB[3] - stepB * delta));
			}
		}
		return colors;
	}

}
